<template>
	<view class="goods-list" :class="[activity.config.style]" v-if="isReady">
		<!-- 顶部固定部分 -->
		<view class="top-warp">
			<!--顶部导航菜单-->
			<Search class="searchbox" title="搜索组件" type="search" :keyword="keyword" :config="activity.config"/>
			<view class="tabs-block">
				<view class="tabs">
					<ListTabs v-model="tabIndex" :config="activity.config" :tabs="tabs" @change="tabChange"></ListTabs>
				</view>
				<view class="attribute" @click="showDrawer('showRight')">
					<text class="text">筛选</text>
					<text class="iconfont">&#xe68a;</text>
				</view>
			</view>
		</view>
		<!--筛选抽屉部分-->
		<view>
			<template v-if="loadAttribute">
			<Attribute ref="attributeDrawer" page="knowledge" :urlParams="urlParams" :config="activity.config"></Attribute>
			</template>
		</view>
		<!--数据列表部分-->
		<view class="list-section uni-clearfix">
			<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" :top="mescrollTop" :bottom="mescrollBottom" :down="downOption" @down="downCallback" @up="upCallback">
				<!-- 数据列表 -->
				<view class="list rank0">
					<view class="item" v-for="(item,index) in list" :key="index" @click="goToDetailLink(item.id)">
						<view class="content">
							<view class="cover">
								<image mode="widthFix" :src="item.cover_400" @load="imageLoad" class="image"></image>
							</view>
							<view class="info">
								<view class="title text-ellipsis-2">{{item.title}}</view>
								<view class="price-info">
									<view class="score-icon">
										￥
									</view>
									<view class="price">
										<text class="pointback" v-if="item.price > 0">{{item.price}}</text>
										<text class="pointback" v-else>免费</text>
									</view>
								</view>
								<view class="exp-info">
									<view class="item view">
										<uni-icons type="eye" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_view}}</text>
									</view>
									<view class="item sales">
										<uni-icons type="cart-filled" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_sales}}</text>
									</view>
									<view class="item favorites">
										<uni-icons type="star" size="10" color="#999"></uni-icons> 
										<text class="num">{{item.handling_favorites}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
		
		<!--#ifdef H5-->
		<tabBar :config="activity.config" />
		<!--#endif-->
		<!--登录框-->
		<Login ref="login" :config="activity.config" @getUserInfo="getUserInfo"/>
	</view>
</template>

<script>
	import Base from '@/muu_activity/common/base.vue';
	import Search from '@/common/diys/Search/Search.vue' //搜索组件
	import ListTabs from "@/common/components/listTabs"; //列表tab
	import Attribute from "../common/attribute"; //筛选抽屉
	import TabBar from "../common/tabBar.vue" //底部导航

	export default {
		extends:Base,
		components: {
			Search,
			ListTabs,
			Attribute,
			TabBar,
		},
		data() {
			return {
				urlParams: {}, //url传递过来的参数
				order_field: '', // 排序类型
				order_type: '', // 排序方向
				loadAttribute: false,
				keyword: '',
				page: 1, //当前页码
				list: [], //列表数据
				tabs: ['默认', '浏览量', '热卖', '价格'],
				tabIndex: 0 ,// tab下标
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
				mescrollTop: 190,
				mescrollBottom: 0,
				userInfo: {},
				ordersTotal: 0, //小店订单数量
				favoritesTotal: 0, //小店收藏数量
				historyTotal: 0, //小店浏览记录数量
			}
		},
		async onLoad(option) {
			this.urlParams = option
			await this.$onLaunched;
			await this.baseIsReady()
			this.loadAttribute = true
			//获取已登录用户数据
			this.getUserInfo()
		},
		created() {
			// #ifdef H5
			this.mescrollBottom = 102;
			// #endif
		},

		methods: {
			
			// 打开筛选抽屉
			showDrawer(e) {
				//触发子组件内方法
				this.$refs.attributeDrawer.$refs[e].open()
			},
			imageLoad: function(e) {
				//console.log(e.detail)
			},
			//上拉加载的回调
			upCallback: function() {
				let that = this
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
				//获取首页数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/activity'
					//#endif
					//#ifdef H5
					url = 'entry/site/activity'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						//console.log(res.data.code);
						if (res.data.code == 200) {
							let list = res.data.data.data
							that.list = that.list.concat(list);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(list.length);
							that.page = that.page + 1
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				
				this.page = 1
				let data = this.urlParams
					data.action = 'lists'
					data.page = this.page
				//获取分页列表首页数据
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/activity'
					//#endif
					//#ifdef H5
					url = 'entry/site/activity'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.list = []
							that.list = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.list.length);
							that.page = 2
						}
					}
				})
			},
			//点击空布局按钮的回调
			emptyClick(){
				uni.showToast({
					title:'点击了按钮,具体逻辑自行实现'
				})
			},
			// 切换TAB
			tabChange(e) {
				if(e == 0){
					this.urlParams.order_field = this.order_field
					this.urlParams.order_type = this.order_type
				}
				if(e == 1){
					this.urlParams.order_field = 'view'
					this.urlParams.order_type = 'DESC'
				}
				if(e == 2){
					this.urlParams.order_field = 'sales'
					this.urlParams.order_type = 'DESC'
				}
				if(e == 3){
					this.urlParams.order_field = 'price'
					this.urlParams.order_type = 'ASC'
				}
				this.page = 1
				this.list = []// 先置空列表,显示加载进度
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			//路由至详情页
			goToDetailLink: function(id){
				let urlParams = 'id='+id
				let url = 'detail?' + urlParams
				//console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
			/**
			 * 获取已登录用户数据
			 */
			getUserInfo: function(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {}
				this.userInfo = userInfo
			},
			handlePrice(price,index){
				return price.split(".")[index];
			}
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	.top-nav {
		display: flex;
		padding: 10rpx;
		.item {
			flex: 1;
			text-align: center;
			font-size: 28rpx;
			color: #fff;
			.title {
				font-size: 26rpx;
			}
		}
	}
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		
		overflow: hidden;
		.tabs-block {
			display: flex;
			flex-wrap: wrap;
			padding: 13rpx 20rpx;
			padding-top: 0;
			.tabs {
				flex: 1;
				.image{
					width:26rpx;
					height: 24rpx;
				}
			}	
			.attribute {
				width: 120rpx;
				height: 76rpx;
				line-height: 76rpx;
				text-align: right;
				display: flex;
				.image {
					width: 28rpx;
					height: 28rpx;
					vertical-align: top;
					margin: 10rpx;
					margin-top: 22rpx;
				}
				.text {
					flex: 1;
					display: block;
					font-size: 26rpx;
					color: #333;
					vertical-align: top;
					font-weight: 600;
				}
			}
		}
	}

	/*横排*/
	.list.rank0 {
		/*小图*/
		display: flex;
		flex-wrap: wrap;
		margin-top: 0;
		background-color: #f5f5f7;
		>.item {
			box-sizing:border-box;
			width: calc(50% - 20rpx);
			background-color: #fff;
			margin: 10rpx 0 10rpx 14rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 10rpx rgba(0,0,0,.02);
			overflow: hidden;
			.content {
				.cover {
					position: relative;
					overflow: hidden;
					.image {
						width: 100%;
						border-radius: 14rpx 14rpx 0 0;
						display: block;
					}
				}
				.info {
					padding: 10rpx 20rpx 20rpx 20rpx;
					.title {
						width: 301rpx;
						font-size: 24rpx;
						color: #2d2f2e;
						font-weight: 400;
						height: 56rpx;
						line-height: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.price-info {
						margin-top: 10rpx;
						font-size: 24rpx;
						display: flex;
						.score-icon {
							width: 30rpx;
							color: #999;
							line-height: 40rpx;
						}
						.price {
							font-size: 30rpx;
							font-weight: 700;
							margin: 0;
							font-family: 'DIN-Medium, DIN';
						}
					}
					.exp-info {
						margin-top: 10rpx;
						color: #999;
						font-size: 24rpx;
						.item {
							display: inline;
							margin: 0;
							.num {
								margin: 0 10rpx;
							}
						}
					}
				}
			}
		}
	}
	/*竖排*/
	.list.rank1 {
		/*小图*/
		display: flex;
		flex-wrap: wrap;
		.item {
			width: 100%;
			.content {
				padding: 10rpx;
				display: flex;
				flex-wrap: wrap;
				.cover {
					position: relative;
					width: 280rpx;
					border-radius: 10rpx;
					overflow: hidden;
					.image {
						width: 100%;
						height: auto;
						display: block;
					}
				}
				.info {
					padding-left: 20rpx;
					width: calc(100% - 300rpx);
					.title {
						font-size: 28rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
						
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.price {
						margin-top: 10rpx;
						font-size: 24rpx;
					}
					.exp-info {
						margin-top: 10rpx;
						color: #999;
						font-size: 24rpx;
						.item {
							display: inline;
							.num {
								margin: 0 10rpx;
							}
						}
					}
				}
			}
		}
	}
	
	/*页面风格设计*/
	.Blue {
		.top-nav {
			background-color: $Blue;
		}
		.price {
			color: $Blue;
		}
	}
	.Green {
		.top-nav {
			background-color: $Green;
		}
		.price {
			color: $Green;
		}
	}
	.LightRed {
		.top-nav {
			background-color: $LightRed;
		}
		.price {
			color: $LightRed;
		}
	}
	.Orange {
		.top-nav {
			background-color: $Orange;
		}
		.price {
			color: $Orange;
		}
	}
	.LightPink {
		.top-nav {
			background-color: $LightPink;
		}
		.price {
			color: $LightPink;
		}
	}
	.Magenta {
		.top-nav {
			background-color: $Magenta;
		}
		.price {
			color: $Magenta;
		}
	}
</style>
